import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
import { ATeamCombat } from "GameLoop/WorldState/EntityManager/Entitys";
import { ETeamState } from "GameLoop/WorldState/EntityManager/Entitys/team";
import { CacheDataHostiles } from "GameLoop/WorldState/PollingStation/PollingTasks/RoomItem/CacheHostiles";
import { destoryPriority } from "utils/constants/priorityconstant";
import { PositionHelper } from "utils/position/PositionHelper";
import { DefendStrategy } from "./AttackStrategy";

export class DefendTaskCheck
{
    // 1.自动更新战斗策略

    // 2.自动组队

    // 3.自动分配目标

    public static  check(room:Room,task:ITask<ITeamTaskData<"defend">>):ETaskState
    {
         this.checkPlan(room,task);
         this.checkRecombineTeam(room,task);
        return  this.checkAssignTarget(room,task);
    }
    /**
     * 更新策略
     * @param room 
     * @param task 
     * @returns 
     */
    private static checkPlan(room:Room,task:ITask<ITeamTaskData<"defend">>):ETaskState
    {
        if(task.data.arg.strategy == "normal")
        {
            
        }
        return ETaskState.Running;
    }

    /**
     * 更新小队组合拆分
     * @param room 
     * @param task 
     * @returns 
     */
    private static checkRecombineTeam(room:Room,task:ITask<ITeamTaskData<"defend">>):ETaskState
    {
        // 先只写小队的删除吧
        for(let k in task.data.teams)
        {
            for(let i=0;i<task.data.teams[k as EntityType].length;)
            {
                const t = task.data.teams[k as EntityType][i];
                const team = room.getEntityByID (t) as ATeamCombat;
                if(!team || team.getNodeMemory().teamState == ETeamState.Dead)
                {
                    // 小队扑街就删除
                    room.removeEntityByID(t);
                    task.data.teams[k as EntityType].splice(i,1);
                }
                else
                {
                    i++;
                }
            }
        }
        // 其他状态的小队重组。另外计算
        return ETaskState.Running;
    }

    /**
     * 更新进攻目标
     * @param room 
     * @param task 
     * @returns 
     */
    private static checkAssignTarget(room:Room,task:ITask<ITeamTaskData<"defend">>):ETaskState
    {
        return DefendStrategy.updateTarget(room,task);
        // return ETaskState.Running;
    }
 
}